<?php

class AjaxController extends Controller
{
	public function actionAutoCheckOnline()
	{
		$status = AuthenticateController::checkOnline();
		echo $status;
	}

	public function actionAutoUpdateMessenger()
	{
		$user_id = Yii::app()->user->getId();
		$status = MessengerController::updateLastLogin($user_id);
		if($status){
			//Get online
			$data['messenger'] = MessengerController::getFriendsOnline($user_id);

			$contentRight   = $this->renderPartial('//page/_aside-chat',array('data'=>$data),true);
			echo $contentRight;
		}
		else
			echo "Thất bại";
	}

	public function actionReadNotification()
	{
		if(isset($_POST['notification']['type']))
		{
			$type = $_POST['notification']['type'];
			$user_id = Yii::app()->user->getId();
			switch ($type) {
				case 'notificationFriend':
					echo FriendshipController::allRead($user_id);
					break;
				case 'notificationMessage':
					$friend_id = $_POST['notification']['friend_id'];
					echo MessengerController::messengerRead($user_id,$friend_id);
					break;
				case 'notificationNews':
					echo NotificationController::allRead($user_id);
					break;
			}
		}
	}

	public function actionRequestFriendship()
	{
		$status = FriendshipController::requestFriendship();
		echo $status;
		if($status == ''){
			$status = NotificationController::addNotification_Friend();
			echo $status;
		}
	}

	public function actionAutoCheckNotification()
	{
		if(isset($_POST['quantityUnread'])){
			$change = false;
			$data = array(
				'friendship'=>'',
                'messenger'=>'',
                'news'=>'',
                );
			$user_id = Yii::app()->user->getId();
			$quantityUnread['friendship'] = FriendshipController::getQuantityUnread($user_id);
			$quantityUnread['messenger'] = MessengerController::getQuantityUnread($user_id);
			$quantityUnread['news'] = NotificationController::getQuantityUnread($user_id);
			if($quantityUnread['friendship'] != $_POST['quantityUnread']['friendship']){
				ob_start();
				$this->renderPartial('//ajax/_notification-friendship');
				$data['friendship']['html'] = ob_get_clean();
				$data['friendship']['quantity'] = $quantityUnread['friendship'];
				$change = true;
				$out = ob_get_clean();
			}
			if($quantityUnread['messenger'] != $_POST['quantityUnread']['messenger']){
				ob_start();
				$this->renderPartial('//ajax/_notification-messenger');
				$data['messenger']['html'] = ob_get_clean();
				$data['messenger']['quantity'] = $quantityUnread['messenger'];
				$change = true;
				$out = ob_get_clean();
			}
			if($quantityUnread['news'] != $_POST['quantityUnread']['news']){
				ob_start();
				$this->renderPartial('//ajax/_notification-news');
				$data['news']['html'] = ob_get_clean();
				$data['news']['quantity'] = $quantityUnread['news'];
				$change = true;
				$out = ob_get_clean();
			}
			if($change)
				echo json_encode($data);
		}
	}

	function actionListMusicControl(){
		if(isset($_POST['action'])){
			$this->renderPartial('//ajax/_add-content-feed');
		}
	}

	function actionSendRequestFriendShip(){
		if(isset($_POST['friend_id'])){
			$friend_id = $_POST['friend_id'];
			$user_id = Yii::app()->user->getId();
			$check = FriendshipController::sendRequest($user_id,$friend_id);
			if($check == ''){
				echo 'Thất bại';
			}else{
				echo 'Thành công';
			}
		}
	}

}